﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BYS.XiAnComDept.App
{
    public class ValidateForm
    {
        public string ValidControls(IEnumerable<DependencyObject> controls)
        {
            foreach (var inputControl in controls)
            {
                if (Validation.GetHasError(inputControl))
                {
                    return Validation.GetErrors(inputControl).First().ErrorContent.ToString();
                }
            }
            return string.Empty;
        }

        public string ValidControls(DependencyObject ContainerOrControl)
        {
            if (ContainerOrControl != null)
            {
                if (Validation.GetErrorTemplate(ContainerOrControl) !=null &&Validation.GetErrorTemplate(ContainerOrControl).HasContent)
                {
                    if (Validation.GetHasError(ContainerOrControl))
                    {
                        return Validation.GetErrors(ContainerOrControl).First().ErrorContent.ToString().Trim();
                    }
                    return string.Empty;
                }
                int childCount = VisualTreeHelper.GetChildrenCount(ContainerOrControl);
                for (var i = 0; i < childCount; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(ContainerOrControl, i);
                    string errorMsg = ValidControls(child);
                    if (errorMsg.Length != 0)
                    {
                        return errorMsg;
                    }
                }
            }
            return string.Empty;
        }
    }
}
